package com.upcode.weighing.vo.sms;

import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author : liqi
 * Date: 2018-05-17
 * Time: 14:51
 */
public class SendSmsUtils {

    private  static final Integer APP_ID = 1400237750;

    private static final String APP_KEY = "68a2f89e5adcc5d587adae88fb43f33a";

    /**
     * 单条发送短息
     * @param vo
     * @return
     */
    public static SendRespVo sendSingle(SingleRequestVo vo){
        SmsSingleSender ssender = new SmsSingleSender(APP_ID, APP_KEY);
        try {
            SmsSingleSenderResult result = ssender.sendWithParam("86", vo.getMobile(),
                    vo.getTemplateId(), vo.getParam(), vo.getSign(),"", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            return new SendRespVo(result.result,result.errMsg);
        } catch (HTTPException e) {
            e.printStackTrace();
            return new SendRespVo(500,"http请求异常");
        } catch (IOException e) {
            e.printStackTrace();
            return new SendRespVo(500,"IO异常");
        }
    }

    /**
     * 批量发送短息
     * @param vo
     * @return
     */
    public static SendRespVo sendBatch(BatchRequestVo vo){
        SmsMultiSender ssender = new SmsMultiSender(APP_ID, APP_KEY);
        try {
            SmsMultiSenderResult result =  ssender.send(0, "86", vo.getPhoneList(),
                    vo.getMsg(), "", "");
            return new SendRespVo(result.result,result.errMsg);
        } catch (HTTPException e) {
            e.printStackTrace();
            return new SendRespVo(500,"http请求异常");
        } catch (IOException e) {
            e.printStackTrace();
            return new SendRespVo(500,"IO异常");
        }
    }

}
